package com.t11;

/**
 * Created by xuyiwei on 2018/7/22.
 */
import java.util.*;
import net.mindview.util.*;
import static net.mindview.util.Print.*;
public class E16_Vowels {
    private final static Set<Character> vowels =
            new HashSet<Character>(Arrays.asList('a', 'e', 'o', 'u',
                    'i', 'A', 'E', 'O', 'U', 'I'));
    public static void updateStatus(Map<Character,Integer> map,Character letter){
        Character ch = Character.toLowerCase(letter);
        Integer freq = map.get(ch);
        map.put(ch, freq == null ? 1 : freq + 1);
    }
    public static void main(String[] args) {
        HashSet<String> processedWords = new HashSet<String>();
        Map<Character,Integer> wordStatus = new HashMap<>();
        Map<Character,Integer> allStatus = new HashMap<>();

        int fileVowels = 0;
        int wordVowels;
        for(String word :
                new TextFile("src/com/t11/E16_Vowels.java", "\\W+")) {
            wordVowels = 0;
            wordStatus.clear();
            for(char letter : word.toCharArray())
                if(vowels.contains(letter)){
                    wordVowels++;
                    updateStatus(wordStatus,letter);
                    updateStatus(allStatus,letter);

                }

            if(!processedWords.contains(word)) {
                processedWords.add(word);
                print("Vowels in " + word + ": " + wordStatus);
            }
            fileVowels += wordVowels;
        }
        print("*************************");
        print("Vowels in the whole file: " + allStatus);
    }
}